package com.zh.note.huawei.onehundred.最小循环子数组;

import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import java.util.stream.Collectors;

/**
 * @Author: zhanghuan
 * @date: 2023/9/12 16:01
 * @description: create it
 * <p>
 * 9
 * 1 2 1 1 2 1 1 2 1
 */
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        // int n = Integer.parseInt(sc.nextLine());
        // String line = sc.nextLine();
        int n = 9;
        String line = "1 2 1 1 2 1 1 2 1";
        List<Integer> list = Arrays.stream(line.split(" "))
                .map(s -> Integer.parseInt(s)).collect(Collectors.toList());
        // int[] res = solution(list);
        solution2(list, n, line);

        // int m = res[n - 1];
        // int length = n % (n - m) == 0 ? n - m : n;
        // for (int i = 0; i < length; i++) {
        //     System.out.print(list.get(i));
        //     if (i < length - 1) {
        //         System.out.print(" ");
        //     }
        // }


    }

    private static void solution2(List<Integer> dataList, int n, String line) {
        for (int i = 1; i < n; i++) {
            if (n % i == 0) {
                int k = n / i;
                List<Integer> list = dataList.subList(0, i);
                // 拼接成子串
                String subStr = list.stream().map(String::valueOf).collect(Collectors.joining(" "));
                StringBuilder sb = new StringBuilder();
                for (int j = 0; j < k; j++) {
                    sb.append(subStr).append(" ");
                }
                if (sb.toString().trim().equals(line)) {
                    System.out.println(subStr);
                    return;
                }
            }
        }
        System.out.println(line);


    }

    private static int[] solution(List<Integer> dataList) {
        int n = dataList.size();
        int[] res = new int[n];
        int k = 0;
        for (int i = 1; i < n; i++) {
            while (k > 0 && !dataList.get(i).equals(dataList.get(k))) {
                k = res[k - 1];
            }
            if (dataList.get(i).equals(dataList.get(k))) {
                k++;
            }
            res[i] = k;
        }
        return res;
    }
}
